var userId = ly.getUrlParam("userId");
var commonArr = [
  { "value": "0", "display": "非常好，完全忘记在治疗" },
  { "value": "1", "display": "比较满意，比常规治疗好" },
  { "value": "2", "display": "一般，和常规治疗差不多" },
  { "value": "3", "display": "不好，有恶心和头晕" }
];
var ifTreat;
var infoId;
var treatMianId;

$(function() {

  $(".form").getFormTwo({
    id: userId
  }, function(data) {
    $("#sex").val(data.medicalUserInfo.sex == 0 ? '男' : '女');
    ifTreat = data.ifTreat; // 是否有信息
    if (ifTreat == 'no') {
      // $("#save-btn").show();
      $("#common").getSelect({
        type: "radio",
        asName: "common",
        list: commonArr
      })
    } else {
      // $("#save-btn").hide();
      infoId =  data.MedicalVrTreatInfo.id;
      treatMianId =  data.MedicalVrTreatInfo.medicalVrBodyInfo.id;
      $("#common").getSelect({
        type: "radio",
        asName: "common",
        list: commonArr,
        default: data.MedicalVrTreatInfo.common
      })
    }
  });

  //提交
  $("body").on("click", "#save-btn", function(event) {
    event.preventDefault();
    var success = $('.form').validate('submitValidate');
    if (success) {
      //验证通过提交表单
      submitFom();
    }
  })

});

/*
 * 提交表单
 */
function submitFom() {
  var treatId = $("#treatId").val();
  var asaStep = $("#asaStep").val();
  var treatName = $("#treatName").val();

  var beforeBp = $("#beforeBp").val();
  var beforeHr = $("#beforeHr").val();
  var beforeRr = $("#beforeRr").val();
  var beforeSpo = $("#beforeSpo").val();

  var middleBp = $("#middleBp").val();
  var middleHr = $("#middleHr").val();
  var middleRr = $("#middleRr").val();
  var middleSpo = $("#middleSpo").val();

  var afterBp = $("#afterBp").val();
  var afterHr = $("#afterHr").val();
  var afterRr = $("#afterRr").val();
  var afterSpo = $("#afterSpo").val();

  var placeName = $("#placeName").val();
  var placeTime = $('#placeTime').val();
  var common = $("#common").find('input[name=common]:checked').val();
  var commonSituation = $('#commonSituation').val();

  var medicalVrBodyInfo = {
    beforeBp: beforeBp,
    beforeHr: beforeHr,
    beforeRr: beforeRr,
    beforeSpo: beforeSpo,
    middleBp: middleBp,
    middleHr: middleHr,
    middleRr: middleRr,
    middleSpo: middleSpo,
    afterBp: afterBp,
    afterHr: afterHr,
    afterRr: afterRr,
    afterSpo: afterSpo
  }

  var obj = {
    "userId": userId,
    'treatId': treatId,
    'asaStep': asaStep,
    'treatName': treatName,
    'medicalVrBodyInfo': medicalVrBodyInfo,
    'placeName': placeName,
    'placeTime': placeTime,
    'common': common,
    'commonSituation': commonSituation
  };

  if (ifTreat == 'yes') {
    obj.id = infoId;
    obj.medicalVrBodyInfo.id = treatMianId;
  }

  var url = site.apiUrl + "/userAdmin/addMedicalVrTreatInfo";
  request({
    url: url,
    data: obj,
    type: "post",
    ajaxform: 'true',
    callback: function(f) {
      if (f.code == 200) {
        ly.alert('编辑成功', function() {
          ly.goto("list.html");
        });
      } else {
        ly.toast(f.desc || f.msg);
      }
    }
  });
}